﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;

namespace EIA.SLClient.ProjectManage
{
    public class LoginStateViewModel : SimpleMvvmToolkit.ViewModelBase<LoginStateViewModel>
    {
        public LoginStateViewModel()
        {
            this.RegisterToReceiveMessages<TStaff>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.LOGIN_STAFF_IDENTIFIED, EP_LoginStaffIdentified);
        }

        private void EP_LoginStaffIdentified(object sender, SimpleMvvmToolkit.NotificationEventArgs<TStaff> e)
        {
            LoginedStaff = e.Data;
        }

        protected TStaff _LoginedStaff;
        /// <summary>
        /// 注释
        /// </summary>
        public TStaff LoginedStaff
        {
            get { return _LoginedStaff; }
            set
            {
                _LoginedStaff = value;
                NotifyPropertyChanged(m => m.LoginedStaff);
            }
        }

        #region LogoutCommand

        private SimpleMvvmToolkit.DelegateCommand _LogoutCommand = null;
        public SimpleMvvmToolkit.DelegateCommand LogoutCommand
        {
            get
            {
                if (_LogoutCommand == null)
                {
                    _LogoutCommand = new SimpleMvvmToolkit.DelegateCommand(Logout, CanExcuteLogout);
                } return _LogoutCommand;
            }
            set { _LogoutCommand = value; NotifyPropertyChanged(m => m.LogoutCommand); }
        }

        public void Logout()
        {
            EIA.WCFService.AuthProxy.AuthenticationProxy proxy = new EIA.WCFService.AuthProxy.AuthenticationProxy();
            string token = Utility.CookiesUtils.GetCookie(EIA.WCFService.ClientProxy.ProxyBase.TOKEN_COOKIE_KEY);
            proxy.Logout(token, (i) =>
            {
                if (i == 0)
                {
                    _LoginedStaff = null;
                    SendMessage(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.LOGIN_SHOW, new SimpleMvvmToolkit.NotificationEventArgs());
                }
                else
                {
                    System.Windows.MessageBox.Show("注销失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                System.Windows.MessageBox.Show("注销发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteLogout()
        {
            return true;
        }

        #endregion

        #region ChangePasswordCommand

        private SimpleMvvmToolkit.DelegateCommand _ChangePasswordCommand = null;
        public SimpleMvvmToolkit.DelegateCommand ChangePasswordCommand
        {
            get
            {
                if (_ChangePasswordCommand == null)
                {
                    _ChangePasswordCommand = new SimpleMvvmToolkit.DelegateCommand(ChangePassword, CanExcuteChangePassword);
                } return _ChangePasswordCommand;
            }
            set { _ChangePasswordCommand = value; NotifyPropertyChanged(m => m.ChangePasswordCommand); }
        }

        public void ChangePassword()
        {
            this.SendMessage(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.CHANGE_PASSWORD, new SimpleMvvmToolkit.NotificationEventArgs());
        }

        public bool CanExcuteChangePassword()
        {
            return true;
        }

        #endregion

    }
}